﻿using Engine;
using Engine.Graphics;
using Engine.Input;
using Engine.Media;
using GameEntitySystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace CommandBlock
{
    /// <summary>
    /// 指令编辑历史界面
    /// </summary>
    public class CommandEditHistoryWidget : CanvasWidget
    {
        public ButtonWidget m_okButton;

        public ButtonWidget m_filterButton;

        public ButtonWidget m_clearButton;

        public LabelWidget m_filterLabel;

        public LabelWidget m_removeLabel;

        public RectangleWidget m_removeRectangle;

        public ListPanelWidget m_instructionlistWidget;

        public CommandEditWidget m_commandEditWidget;

        public bool m_filter;

        public bool m_fastMode;

        public CommandEditHistoryWidget(CommandEditWidget commandEditWidget, Point3 pos, bool fast)
        {
            m_commandEditWidget = commandEditWidget;
            m_commandEditWidget.IsEnabled = false;
            m_fastMode = fast;
            m_filter = true;
            XElement node = ContentManager.Get<XElement>("Widgets/CommandEditHistoryWidget");
            LoadContents(this, node);
            m_instructionlistWidget = Children.Find<ListPanelWidget>("EditHistory.List");
            m_okButton = Children.Find<ButtonWidget>("OkButton");
            m_filterButton = Children.Find<ButtonWidget>("FilterButton");
            m_clearButton = Children.Find<ButtonWidget>("ClearButton");
            m_filterLabel = Children.Find<LabelWidget>("FilterLabel");
            m_removeLabel = Children.Find<LabelWidget>("RemoveLabel");
            m_removeRectangle = Children.Find<RectangleWidget>("RemoveRectangle");
            UpdateItems();
            m_instructionlistWidget.ItemWidgetFactory = delegate (object item)
            {
                HistoryEditItem historyEditItem = (HistoryEditItem)item;
                XElement node2 = ContentManager.Get<XElement>("Widgets/HistoryEditItem");
                ContainerWidget containerWidget = (ContainerWidget)LoadWidget(this, node2, null);
                string title = string.Format("{0}   编辑位置:({1})", historyEditItem.About, historyEditItem.Position.ToString());
                Color passColor = historyEditItem.Pass ? new Color(32, 255, 32, 160) : new Color(255, 32, 32, 160);
                containerWidget.Children.Find<RectangleWidget>("PassRectangle").FillColor = passColor;
                containerWidget.Children.Find<LabelWidget>("HistoryEditItem.About").Text = title;
                containerWidget.Children.Find<LabelWidget>("HistoryEditItem.Line").Text = historyEditItem.Line;
                return containerWidget;
            };
            m_instructionlistWidget.ItemClicked += delegate (object item)
            {
                if (m_instructionlistWidget.SelectedItem == item)
                {
                    HistoryEditItem historyEditItem = (HistoryEditItem)item;
                    if (historyEditItem != null)
                    {
                        CommandData commandData = new CommandData((m_fastMode ? Point3.Zero : pos), historyEditItem.Line);
                        commandData.TrySetValue();
                        m_commandEditWidget.Initialize();
                        m_commandEditWidget.SetCommandDataValue(commandData);
                        m_commandEditWidget.ChangeExplains();
                        m_commandEditWidget.GetDynamicCommanndWidget();
                        m_commandEditWidget.SetCommandWidgetValue(commandData);
                        CloseCurrentWidget();
                    }
                }
            };
        }

        public override void Update()
        {
            m_filterLabel.Text = m_filter ? "收藏" : "全部";
            m_removeLabel.Text = m_filter ? "移除" : "清空";
            if (m_filter)
            {
                if (m_instructionlistWidget.SelectedItem == null)
                {
                    m_removeRectangle.FillColor = new Color(64, 64, 64);
                    m_clearButton.IsEnabled = false;
                }
                else
                {
                    m_removeRectangle.FillColor = new Color(100, 100, 100);
                    m_clearButton.IsEnabled = true;
                }
            }
            else
            {
                m_removeRectangle.FillColor = new Color(100, 100, 100);
                m_clearButton.IsEnabled = true;
            }
            if (m_okButton.IsClicked || base.Input.Cancel || base.Input.Back)
            {
                CloseCurrentWidget();
            }
            if (m_filterButton.IsClicked)
            {
                m_filter = !m_filter;
                UpdateItems();
            }
            if (m_clearButton.IsClicked)
            {
                if (m_filter)
                {
                    HistoryEditItem historyEditItem = (HistoryEditItem)m_instructionlistWidget.SelectedItem;
                    InstructionManager.RemoveHistoryItem(historyEditItem);
                    UpdateItems();
                }
                else
                {
                    DialogsManager.ShowDialog(m_commandEditWidget.m_componentPlayer.GuiWidget, new MessageDialog("是否清空所有指令历史记录", "注意：收藏的指令也会被清空", "确定", "取消", delegate (MessageDialogButton button)
                    {
                        if (button == MessageDialogButton.Button1)
                        {
                            InstructionManager.HistoryEditInstructions.Clear();
                            InstructionManager.CollectionInstructions.Clear();
                            m_instructionlistWidget.ClearItems();
                        }
                    }));
                }
            }
        }

        public void UpdateItems()
        {
            m_instructionlistWidget.ClearItems();
            if (m_filter)
            {
                List<HistoryEditItem> items = InstructionManager.CollectionInstructions;
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    if (m_fastMode && items[i].Condition) continue;
                    m_instructionlistWidget.AddItem(items[i]);
                }
            }
            else
            {
                List<HistoryEditItem> items = InstructionManager.HistoryEditInstructions;
                int num = 0;
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    if (m_fastMode && items[i].Condition) continue;
                    m_instructionlistWidget.AddItem(items[i]);
                    if ((++num) >= 50) break;
                }
            }
        }

        public void CloseCurrentWidget()
        {
            base.ParentWidget.RemoveChildren(this);
            Time.QueueTimeDelayedExecution(Time.RealTime + 0.5f, delegate
            {
                m_commandEditWidget.IsEnabled = true;
            });
        }
    }
}